/* 	
	Replication for Banda, Carsey, and Curiel's "Incumbency Status and 
	Candidate Responsiveness to Voters in Two-Stage Elections"
	
	4/27/2019
	
To use the style of plots presented in the paper, use the following two commands
	if you don't have plottig installed already:

ssc install blindschemes, replace all
set scheme plottig
	
*/

* Open the data
use "data.dta", clear

**************
* Main paper *
**************

* Range of number of Dems and Reps running in primaries
sum numdems numreps if used==1

* Table 1
sum cfscore primaryID generalID incumbent demd female openseat senate ///
	if used==1

* Table 2
xtmixed cfscore c.primaryID##c.incumbent c.generalID##c.incumbent ///
	demd openseat senate female || stateabb: || districtcode: ///
	if primaryIDcount > 9.879503 & generalIDcount > 10.27004
* For the BIC
estat ic

* Figure 2 (uses estimates from Table 2)
margins, dydx(primaryID) at(incumbent=(0 1))
marginsplot, x(incumbent) recast(scatter) ciopts(col(black) ls(2) lw(1)) xlabel(-.2(.1).6, labsize(medsmall)) level(95) ///
	plot1opts(mc("black") msize(3)) ylabel(0 "Non-incumbent" 1 "Incumbent", noticks labsize(medsmall)) ///
	xtitle(" " "Marginal effects", size(medsmall)) ytitle("") ///
	title("Primary election voters") legend(region(lstyle(none))) ysize(4) xsize(4) xline(0, lc(red)) horizontal ///
	yscale(range(-.5 1.5))

margins, dydx(generalID) at(incumbent=(0 1))
marginsplot, x(incumbent) recast(scatter) ciopts(col(black) ls(2) lw(1)) xlabel(-.2(.1).6, labsize(medsmall)) level(95) ///
	plot1opts(mc("black") msize(3)) ylabel(0 "Non-incumbent" 1 "Incumbent", noticks labsize(medsmall)) ///
	xtitle(" " "Marginal effects", size(medsmall)) ytitle("") ///
	title("General election-only voters") legend(region(lstyle(none))) ysize(4) xsize(4) xline(0, lc(red)) horizontal ///
	yscale(range(-.5 1.5)) xline(0.6, lc(white) lp(solid))

* Figure 3 (uses estimates from Table 2)
margins, at(primaryID=(1.7(.1)4.5) incumbent=(0(1)1) (median) _all)
marginsplot, x(primaryID) recast(line) recastci(rline) ylabel(0(.2)1.6, labsize(medsmall)) level(95) ///
	plot1opts(lw(1) lc("black") lp("solid")) ci1opts(lw(1) lc("black") lp("shortdash") lw(.5)) ///
	plot2opts(lw(1) lc(gs6) lp("solid")) ci2opts(lw(1) lc(gs6) lp("shortdash") lw(.5)) ///
	ytitle("Predicted candidate ideology" " ", size(medsmall)) ///
	xtitle(" " "Primary voters ideology", size(medsmall)) xlabel(1.5(.5)4.5, labsize(medsmall)) ///
	title("") legend(region(lstyle(none)) size(medsmall) pos(6) col(2)) ysize(4) xsize(4)
	

************
* Appendix *
************
	
* Table A
xtmixed cfscore c.primaryID##c.incumbent c.generalID##c.incumbent ///
	demd openseat senate female || stateabb: || districtcode: ///
	if primaryIDcount
* For the BIC
estat ic

* Figure 1
margins, dydx(primaryID) at(incumbent=(0 1))
marginsplot, x(incumbent) recast(scatter) ciopts(col(black) ls(2) lw(1)) xlabel(-.2(.1).6, labsize(medsmall)) level(95) ///
	plot1opts(mc("black") msize(3)) ylabel(0 "Non-incumbent" 1 "Incumbent", noticks labsize(medsmall)) ///
	xtitle(" " "Marginal effects", size(medsmall)) ytitle("") ///
	title("Primary election voters") legend(region(lstyle(none))) ysize(4) xsize(4) xline(0, lc(red)) horizontal ///
	yscale(range(-.5 1.5))

margins, dydx(generalID) at(incumbent=(0 1))
marginsplot, x(incumbent) recast(scatter) ciopts(col(black) ls(2) lw(1)) xlabel(-.2(.1).6, labsize(medsmall)) level(95) ///
	plot1opts(mc("black") msize(3)) ylabel(0 "Non-incumbent" 1 "Incumbent", noticks labsize(medsmall)) ///
	xtitle(" " "Marginal effects", size(medsmall)) ytitle("") ///
	title("General election-only voters") legend(region(lstyle(none))) ysize(4) xsize(4) xline(0, lc(red)) horizontal ///
	yscale(range(-.5 1.5))


* Table B (next three models)

* Primary winner interaction model
ta incumbent primarywin if used==1, row

xtmixed cfscore c.primaryID##c.incumbent##c.primarywin c.generalID##c.incumbent##c.primarywin ///
	demd openseat senate female || stateabb: || districtcode: ///
	if primaryIDcount > 9.879503 & generalIDcount > 10.27004
* For the BIC
estat ic

* Figure B
margins, dydx(primaryID) at(incumbent=(0 1) primarywin=(0 1))
marginsplot, x(incumbent primarywin) recast(scatter) ciopts(col(black) ls(2) lw(1)) xlabel(-.6(.2)1.4, labsize(medsmall)) level(95) ///
	ylabel(1 `""Non-incumbent" "loser""' 2 `""Non-incumbent" "winner""' 3 `""Incumbent" "loser""' 4 `""Incumbent" "winner""', noticks labsize(medsmall)) ///
	yscale(range(.5 4.5)) ytitle("") xtitle(" " "Marginal effects", size(medsmall)) ///
	xsize(4) ysize(4) plot1opts(mc("black") msize(3)) horizontal xline(0, lc(red)) title("Primary election voters", size(medsmall)) ///
	xline(1.4, lc(white) lp(solid))

margins, dydx(generalID) at(incumbent=(0 1) primarywin=(0 1))
marginsplot, x(incumbent primarywin) recast(scatter) ciopts(col(black) ls(2) lw(1)) xlabel(-.6(.2)1.4, labsize(medsmall)) level(95) ///
	ylabel(1 `""Non-incumbent" "loser""' 2 `""Non-incumbent" "winner""' 3 `""Incumbent" "loser""' 4 `""Incumbent" "winner""', labsize(medsmall) noticks) ///
	yscale(range(.5 4.5)) ytitle("") xtitle(" " "Marginal effects", size(medsmall)) ///
	xsize(4) ysize(4) plot1opts(mc("black") msize(3)) horizontal xline(0, lc(red)) title("General election-only voters", size(medsmall))
	
* Primary was contested interaction model
ta incumbent contested if used==1, row

xtmixed cfscore c.primaryID##c.incumbent##c.contested c.generalID##c.incumbent##c.contested ///
	demd openseat senate female || stateabb: || districtcode: ///
	if primaryIDcount > 9.879503 & generalIDcount > 10.27004
* For the BIC
estat ic

* Figure C
margins, dydx(primaryID) at(incumbent=(0 1) contested=(0 1))
marginsplot, x(incumbent contested) recast(scatter) ciopts(col(black) ls(2) lw(1)) xlabel(-.8(.2).8, labsize(medsmall)) level(95) ///
	ylabel(1 `""Non-incumbent" "uncontested""' 2 `""Non-incumbent" "contested""' 3 `""Incumbent" "uncontested""' 4 `""Incumbent" "contested""', labsize(medsmall) noticks) ///
	yscale(range(.5 4.5)) ytitle("") xtitle(" " "Marginal effects", size(medsmall)) ///
	xsize(4) ysize(4) plot1opts(mc("black") msize(3)) horizontal xline(0, lc(red)) title("Primary election voters", size(medsmall)) ///
	xline(-.8 .8, lc(white) lp(solid))

margins, dydx(generalID) at(incumbent=(0 1) contested=(0 1))
marginsplot, x(incumbent contested) recast(scatter) ciopts(col(black) ls(2) lw(1)) xlabel(-.8(.2).8, labsize(medsmall)) level(95) ///
	ylabel(1 `""Non-incumbent" "uncontested""' 2 `""Non-incumbent" "contested""' 3 `""Incumbent" "uncontested""' 4 `""Incumbent" "contested""', labsize(medsmall) noticks) ///
	yscale(range(.5 4.5)) ytitle("") xtitle(" " "Marginal effects", size(medsmall)) ///
	xsize(4) ysize(4) plot1opts(mc("black") msize(3)) horizontal xline(0, lc(red)) title("General election-only voters", size(medsmall)) ///
	xline(-.8 .8, lc(white) lp(solid))


* Office type interaction model
ta incumbent senate if used==1, row

xtmixed cfscore c.primaryID##c.incumbent##c.senate c.generalID##c.incumbent##c.senate ///
	demd openseat female || stateabb: || districtcode: ///
	if primaryIDcount > 9.879503 & generalIDcount > 10.27004
* For the BIC
estat ic

* Figure D
margins, dydx(primaryID) at(incumbent=(0 1) senate=(0 1))
marginsplot, x(incumbent senate) recast(scatter) ciopts(col(black) ls(2) lw(1)) xlabel(-1.2(.4)2, labsize(medsmall)) level(95) ///
	ylabel(1 `""Non-incumbent" "House""' 2 `""Non-incumbent" "Senate""' 3 `""Incumbent" "House""' 4 `""Incumbent" "Senate""', labsize(medsmall) noticks) ///
	yscale(range(.5 4.5)) ytitle("") xtitle(" " "Marginal effects", size(medsmall)) ///
	xsize(4) ysize(4) plot1opts(mc("black") msize(3)) horizontal xline(0, lc(red)) title("Primary election voters", size(medsmall)) ///
	xline(-1.2 2, lc(white) lp(solid))

margins, dydx(generalID) at(incumbent=(0 1) senate=(0 1))
marginsplot, x(incumbent senate) recast(scatter) ciopts(col(black) ls(2) lw(1)) xlabel(-1.2(.4)2, labsize(medsmall)) level(95) ///
	ylabel(1 `""Non-incumbent" "House""' 2 `""Non-incumbent" "Senate""' 3 `""Incumbent" "House""' 4 `""Incumbent" "Senate""', labsize(medsmall) noticks) ///
	yscale(range(.5 4.5)) ytitle("") xtitle(" " "Marginal effects", size(medsmall)) ///
	xsize(4) ysize(4) plot1opts(mc("black") msize(3)) horizontal xline(0, lc(red)) title("General election-only voters", size(medsmall))
